package site.xuxingcan.autodownload;

import android.os.Environment;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

/**
 * Created by Administrator on 2016/9/24.
 */
public class DownloadThread extends Thread {
    private ArrayList<String> urlList = null;
    private String urlStr = null;
    private int count;
    private boolean isFinish = false;
    private int sleepTime = 1;
    private boolean isStop = false;

    public DownloadThread(String urlStr, int count, int sleepTime) {
        this.urlStr = urlStr;
        this.count = count;
        this.sleepTime = sleepTime;
    }

    public DownloadThread(ArrayList<String> urlList, int count, int sleepTime) {
        this.urlList = urlList;
        this.count = count;
        this.sleepTime = sleepTime;
    }

    public void setStop(boolean stop) {
        isStop = stop;
    }

    public boolean isFinish() {
        return isFinish;
    }

    @Override
    public void run() {
        super.run();
        isFinish = false;
        if (urlStr != null) {
            while (true) {
                try {
                    File file = new File(Environment.getExternalStorageDirectory(), "file" + count);
                    if (file.exists()) {
                        file.delete();
                    }
                    URL url = new URL(urlStr);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setConnectTimeout(30 * 1000);
                    conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
                    InputStream inputStream = conn.getInputStream();
                    FileOutputStream fos = new FileOutputStream(file);
                    byte[] buffer = new byte[1024];
                    int len = 0;
                    while ((len = inputStream.read(buffer)) != -1) {
                        fos.write(buffer, 0, len);
                        if (isStop) {
                            break;
                        }
                    }
                    if (isStop) {
                        break;
                    }
                    fos.close();
                    inputStream.close();
                    long t = sleepTime * 1000;
                    if (t <= 0) {
                        t = 1;
                    }
                    Thread.sleep(t);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else if (urlList != null && urlList.size() != 0) {
            while (true) {
                for (String u : urlList) {
                    try {
                        File file = new File(Environment.getExternalStorageDirectory(), "file" + count);
                        if (file.exists()) {
                            file.delete();
                        }
                        URL url = new URL(u);
                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                        conn.setConnectTimeout(30 * 1000);
                        conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
                        InputStream inputStream = conn.getInputStream();
                        FileOutputStream fos = new FileOutputStream(file);
                        byte[] buffer = new byte[1024];
                        int len = 0;
                        while ((len = inputStream.read(buffer)) != -1) {
                            fos.write(buffer, 0, len);
                            if (isStop) {
                                break;
                            }
                        }
                        if (isStop) {
                            break;
                        }
                        fos.close();
                        inputStream.close();
                        long t = sleepTime * 1000;
                        if (t <= 0) {
                            t = 1;
                        }
                        Thread.sleep(t);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                if (isStop) {
                    break;
                }
            }
        }
        isFinish = true;
    }
}
